#include <iostream>
#include <pthread.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/types.h>

#define NUM 1000

//多个线程进行抢票
static int tickets = NUM;

void* GrabTickets(void*)
{
  pid_t tid = syscall(SYS_gettid);
  while(true){
    if(tickets > 0){
      usleep(1000);
      printf("thread[%d], tickets->%d\n", (int)tid, tickets);
      tickets--;
    }
    else break;
  }

  return nullptr;
}

int main()
{
  pthread_t tids[5];
  for(int i = 0; i < 5; i++){
    pthread_create(&tids[i], nullptr, GrabTickets, nullptr);
  }

  for(int i = 0; i < 5; i++){
    pthread_join(tids[i], nullptr);
  }
  return 0;
}

